<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/8/26
 * Time: 15:16
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class  Order extends Base {
    public function index(){
        if (request()->isAjax()) {
            extract(input());
            $map =array();
            if(isset($keyword)&&$keyword!="")
            {
                $map['uuu.user_real_name|uuu1.user_username'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($keyword1)&&$keyword1!="")
            {
                $map['uuu.user_phone|uuu1.user_phone'] = ['like',"%" . $keyword1 . "%"];
            }
            if(isset($keyword2)&&$keyword2!="")
            {
                $map['a.order_num'] = ['like',"%" . $keyword2 . "%"];
            }

            if(isset($province)&&$province!="")
            {
                $map['a.order_go_sheng'] =$province;
            }
            if(isset($city)&&$city!="")
            {
                $map['a.order_go_shi'] =$city;
            }
            if(isset($district)&&$district!="")
            {
                $map['a.order_go_qu'] =$district;
            }


            if(isset($order_to_sheng)&&$order_to_sheng!="")
            {
                $map['a.order_to_sheng'] =$order_to_sheng;
            }
            if(isset($order_to_shi)&&$order_to_shi!="")
            {
                $map['a.order_to_shi'] =$order_to_shi;
            }
            if(isset($order_to_qu)&&$order_to_qu!="")
            {
                $map['a.order_to_qu'] =$order_to_qu;
            }

            if(isset($status)&&$status!="")
            {
                if($status==1){
                    $map['a.order_status'] = array('between',array(2,7));
                }elseif ($status==2){
                    $map['a.order_status'] = array('between',array(8,9));
                }
            }else{
                $map['a.order_status'] =array('>',2);
            }
            $map['a.order_type'] =array('IN',array(1,2));
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 50;
            $count = Db::name('order')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.order_dirver_user','left')
                ->join('up_u_user uuu1','uuu1.user_id = a.order_qy_user','left')
                ->join('up_area ua','ua.district_id = a.order_go_sheng','left')
                ->join('up_area ua1','ua1.district_id = a.order_go_shi','left')
                ->join('up_area ua2','ua2.district_id = a.order_to_sheng','left')
                ->join('up_area ua3','ua3.district_id = a.order_to_shi','left')
                ->where($map)
                ->count();// 获取总条数
            $lists = Db::name('order')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.order_dirver_user','left')
                ->join('up_u_user uuu1','uuu1.user_id = a.order_qy_user','left')
                ->join('up_area ua','ua.district_id = a.order_go_sheng','left')
                ->join('up_area ua1','ua1.district_id = a.order_go_shi','left')
                ->join('up_area ua2','ua2.district_id = a.order_to_sheng','left')
                ->join('up_area ua3','ua3.district_id = a.order_to_shi','left')
                ->page($NowPage, $limits)
                ->where($map)
                ->field('a.*,uuu.user_real_name,uuu.user_phone,uuu1.user_gongsi
                ,uuu1.user_username as qy_username,uuu1.user_phone as qy_phone,
                 ua.district as go_sheng,ua1.district as go_shi,
                        ua2.district as to_sheng,ua3.district as to_shi')
                ->order('a.order_fb_time desc')
                ->select();
            $count2 = Db::name('order')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.order_dirver_user','left')
                ->join('up_u_user uuu1','uuu1.user_id = a.order_qy_user','left')
                ->join('up_area ua','ua.district_id = a.order_go_sheng','left')
                ->join('up_area ua1','ua1.district_id = a.order_go_shi','left')
                ->join('up_area ua2','ua2.district_id = a.order_to_sheng','left')
                ->join('up_area ua3','ua3.district_id = a.order_to_shi','left')
                ->where($map)
                ->column('order_id');// 获取总条数
            foreach ($lists as $key=>&$vo){
                $vo['go_address'] = $vo['go_sheng'].'-'.$vo['go_shi'];
                $vo['to_address'] = $vo['to_sheng'].'-'.$vo['to_shi'];
                if(in_array($vo['order_status'],array(2,3,4,5,6,7))){
                    $vo['order_status_name'] = '进行中';
                }elseif (in_array($vo['order_status'],array(8,9))){
                    $vo['order_status_name'] = '完成';
                }
                //税赋
                $price1 = ($vo['order_freight']/1.09)*0.09;
                //保险成本
                $price2 = $vo['order_freight']*0.06;
                //平台抽成
                if($vo['order_freight']<30000){
                $price3 = $vo['order_freight']*0.0358;
                }elseif ($vo['order_freight']>=30000&&$vo['order_freight']<300000){
                $price3 = $vo['order_freight']*0.0258;
                }elseif ($vo['order_freight']>=300000){
                $price3 = $vo['order_freight']*0.0198;
                }
                $price4 = $vo['order_freight'] - $price1- $price2- $price3;
                $vo['price'] = number_format($price4, 2);
            }
            $str = '';
            if($count2){
                $str = implode($count2,',');
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'count2' => $str,'data' => $lists]);
        }
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        $city = Db::table('up_area')->where('level', 2)->field('district_id, district')->select();
        $district = Db::table('up_area')->where('level', 3)->field('district_id, district')->select();
        $this->assign([
            'province' => $province,
            'city' => $city,
            'district' => $district,
        ]);
        return $this->fetch();
    }
    public function detail(){
        $id = input('param.order_id');
        $map['a.order_id'] = $id;
        $info = Db::name('order')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.order_dirver_user','left')
            ->join('up_u_user uuu1','uuu1.user_id = a.order_qy_user','left')
            ->join('up_area ua','ua.district_id = a.order_go_sheng','left')
            ->join('up_area ua1','ua1.district_id = a.order_go_shi','left')
            ->join('up_area ua2','ua2.district_id = a.order_to_sheng','left')
            ->join('up_area ua3','ua3.district_id = a.order_to_shi','left')
            ->where($map)
            ->field('a.*,uuu.user_real_name,uuu.user_phone,uuu1.user_gongsi
                ,uuu1.user_username as qy_username,uuu1.user_phone as qy_phone,
                 ua.district as go_sheng,ua1.district as go_shi,
                 ua2.district as to_sheng,ua3.district as to_shi')
            ->find();
        $info['go_address'] = $info['go_sheng'].'-'.$info['go_shi'];
        $info['to_address'] = $info['to_sheng'].'-'.$info['to_shi'];
        $info['zxlx_name'] = Db::table('up_car_zxlx')->where('zxlx_id',$info['zxlx_id'])->value('zxlx_name');
        $info['hwlx_name'] = Db::table('up_car_hwlx')->where('hwlx_id',$info['hwlx_id'])->value('hwlx_name');
        $this->assign('res',$info);
        return $this->fetch();
    }

    public function update(){
        if(Request::instance()->isPost()){
            $order_goods_name = input('param.order_goods_name');
            $order_goods_ton = input('param.order_goods_ton');
            $order_goods_m = input('param.order_goods_m');
            $order_go_sheng = input('param.order_go_sheng');
            $order_go_shi = input('param.order_go_shi');
            $order_go_qu = input('param.order_go_qu');
            $order_go_address = input('param.order_go_address');
            $order_go_lon = input('param.order_go_lon');
            $order_go_lat = input('param.order_go_lat');
            $order_to_sheng = input('param.order_to_sheng');
            $order_to_shi = input('param.order_to_shi');
            $order_to_qu = input('param.order_to_qu');
            $order_to_address = input('param.order_to_address');
            $order_to_lon = input('param.order_to_lon');
            $order_to_lat = input('param.order_to_lat');
            $order_zc_time = input('param.order_zc_time');
            $zxlx_id = input('param.zxlx_id');
            $hwlx_id = input('param.hwlx_id');
            $cx_id = input('param.cx_id');
            $order_cc = input('param.order_cc');
            $order_goods_img = input('param.photo');
            $order_zc_goods_img = input('param.photo3');
            $order_xh_goods_img = input('param.photo4');
            $id = input('param.id');
            $data['order_goods_name'] = $order_goods_name;
            $data['order_goods_ton'] = $order_goods_ton;
            $data['order_goods_m'] = $order_goods_m;
            $data['order_go_sheng'] = $order_go_sheng;
            $data['order_go_shi'] = $order_go_shi;
            $data['order_go_qu'] = $order_go_qu;
            $data['order_go_address'] = $order_go_address;
            $data['order_go_lon'] = $order_go_lon;
            $data['order_go_lat'] = $order_go_lat;
            $data['order_to_sheng'] = $order_to_sheng;
            $data['order_to_shi'] = $order_to_shi;
            $data['order_to_qu'] = $order_to_qu;
            $data['order_to_address'] = $order_to_address;
            $data['order_to_lon'] = $order_to_lon;
            $data['order_to_lat'] = $order_to_lat;
            $data['order_zc_time'] = $order_zc_time;
            $data['zxlx_id'] = $zxlx_id;
            $data['hwlx_id'] = $hwlx_id;
            $data['cx_id'] = $cx_id;
            $data['order_cc'] = $order_cc;
            $data['order_goods_img'] = $order_goods_img;
            $data['order_zc_goods_img'] = $order_zc_goods_img;
            $data['order_xh_goods_img'] = $order_xh_goods_img;
            $res = Db::table('up_order')->where('order_id', $id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        $city = Db::table('up_area')->where('level', 2)->field('district_id, district')->select();
        $district = Db::table('up_area')->where('level', 3)->field('district_id, district')->select();
        $id = input('param.order_id');
        $res = Db::table('up_order')->where('order_id',$id)->find();
        $res['cx_name'] = explode(',',$res['cx_id']);
        $res['cc_name'] = explode(',',$res['order_cc']);
        $cc = Db::table('up_car_cclb')->where('is_del',1)->where('is_status',1)->select();
        $cx= Db::table('up_car_cx')->where('is_del',1)->where('is_status',1)->select();
        $hw = Db::table('up_car_hwlx')->where('is_del',1)->where('is_status',1)->select();
        $zx= Db::table('up_car_zxlx')->where('is_del',1)->where('is_status',1)->select();
        $this->assign([
            'province' => $province,
            'city' => $city,
            'district' => $district,
            'id'=>$id,
            'res'=>$res,
            'cc'=>$cc,
            'cx'=>$cx,
            'hw'=>$hw,
            'zx'=>$zx
        ]);
        return $this->fetch();
    }

    public function outExcel(){

        $user_ids = input('ids');
        if($user_ids == ''){
            return json(['code' => 100, 'msg' => '请选择要操作的订单']);
        }
        $lists = Db::name('order')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.order_dirver_user','left')
            ->join('up_u_user uuu1','uuu1.user_id = a.order_qy_user','left')
            ->join('up_area ua','ua.district_id = a.order_go_sheng','left')
            ->join('up_area ua1','ua1.district_id = a.order_go_shi','left')
            ->join('up_area ua2','ua2.district_id = a.order_to_sheng','left')
            ->join('up_area ua3','ua3.district_id = a.order_to_shi','left')
            ->where('order_id', 'IN', $user_ids)
            ->field('a.order_id,a.order_status,a.order_num,a.order_zc_time,
            a.order_dzxy_yf,a.order_freight,uuu.user_username,uuu.user_phone,uuu1.user_gongsi
                ,uuu1.user_username as qy_username,uuu1.user_phone as qy_phone,
                 ua.district as go_sheng,ua1.district as go_shi,
                        ua2.district as to_sheng,ua3.district as to_shi')
            ->order('a.order_fb_time desc')
            ->select();
//        foreach($lists as $k => $v){
//            $lists[$k]['user_phone'] = "\t".$v['user_phone']."\t";
//        }
        foreach ($lists as $key=>&$vo){
            $vo['go_address'] = $vo['go_sheng'].'-'.$vo['go_shi'];
            $vo['to_address'] = $vo['to_sheng'].'-'.$vo['to_shi'];
            if(in_array($vo['order_status'],array(2,3,4,5,6,7))){
                $vo['order_status_name'] = '进行中';
            }elseif (in_array($vo['order_status'],array(8,9))){
                $vo['order_status_name'] = '完成';
            }
            //税赋
            $price1 = ($vo['order_freight']/1.09)*0.09;
            //保险成本
            $price2 = $vo['order_freight']*0.06;
            //平台抽成
            if($vo['order_freight']<30000){
                $price3 = $vo['order_freight']*0.0358;
            }elseif ($vo['order_freight']>=30000&&$vo['order_freight']<300000){
                $price3 = $vo['order_freight']*0.0258;
            }elseif ($vo['order_freight']>=300000){
                $price3 = $vo['order_freight']*0.0198;
            }
            $price4 = $vo['order_freight'] - $price1- $price2- $price3;
            $vo['price'] = number_format($price4, 2);
            $lists[$key]['order_id'] = $vo['order_id'];
            $lists[$key]['order_status_name'] = $vo['order_status_name'];
            $lists[$key]['order_num'] = "\t".$vo['order_num']."\t";
            $lists[$key]['price'] = "\t".$vo['price']."\t";
            $lists[$key]['user_username'] = "\t".$vo['user_username']."\t";
            $lists[$key]['user_phone'] = "\t".$vo['user_phone']."\t";
            $lists[$key]['qy_username'] = "\t".$vo['qy_username']."\t";
            $lists[$key]['qy_phone'] = "\t".$vo['qy_phone']."\t";
            $lists[$key]['go_address'] = "\t".$vo['go_address']."\t";
            $lists[$key]['to_address'] = "\t".$vo['to_address']."\t";
            $lists[$key]['order_zc_time'] = "\t".$vo['order_zc_time']."\t";
            $lists[$key]['order_dzxy_yf'] = "\t".$vo['order_dzxy_yf']."\t";
            $lists[$key]['order_freight'] = "\t".$vo['order_freight']."\t";
        }
        $cellname = [
            ['order_id','ID',20,'LEFT'],
            ['order_status_name','订单状态',20,'LEFT'],
            ['order_num','订单号',20,'LEFT'],
            ['user_gongsi','企业名称',20,'LEFT'],
            ['price','本单获得金额',20,'LEFT'],
            ['user_username','司机昵称',20,'LEFT'],
            ['user_phone','司机号码',20,'LEFT'],
            ['qy_username','发布人姓名',20,'LEFT'],
            ['qy_phone','发布人手机号码',20,'LEFT'],
            ['go_address','出发地',20,'LEFT'],
            ['to_address','目的地',20,'LEFT'],
            ['order_zc_time','装车时间',20,'LEFT'],
            ['order_dzxy_yf','定金金额',20,'LEFT'],
            ['order_freight','整单金额',20,'LEFT'],
        ];
        $res = exportExcel('订单列表', 'Looks', $cellname, $lists);
        return json($res);
    }
}